<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  // https://mp.weixin.qq.com/s?__biz=MzU2Mzk1NzkwOA==&mid=2247485528&idx=1&sn=238414795c64730b8dc3074f9c365e7e&chksm=fc5311a3cb2498b5dde9767d7f720c34ba3684c934cbdaf84d8396e08bd2c86d6b4f0de14437&scene=126&sessionid=1607389331&key=1983696ae0fa3b7d37ff51e7b452945e988ba2a399fa8eaf19886e52914ec92311d11ecb582f0eb39a5e0fa92735efd3078e27a5240ba104ff72f2e3d4a721293e6c8034532ea94a3f97e957fa49c5e29a674042d631efe3554de19030575545a01fb2d23e3b50e7e8db3f8b3e35e64284e35fd1f076fafc405ca7f875f12716&ascene=1&uin=NzU0OTY0Mzk5&devicetype=Windows+10+x64&version=6300002f&lang=zh_CN&exportkey=A4CXBdWG3xHgBSyoUExrd2M%3D&pass_ticket=ubqZET0myd0SyoBaCxCNw%2BKTsH5Ieza7a0msxs5mIJfBVFnw8jxM62AOJoNazOZa&wx_header=0
  // 基本思路：通过for循环遍历数组，找出要搜索的值在数组中的索引，并将其推进新数组
  "use strict"; {
    // 使用for
    let searchBy = (arr, value) => {
      let result = [];
      for (let i = 0, len = arr.length; i < len; i++) {
        if (arr[i] === value) result.push(i);
      }
      return result
    };
  }; {
    // 使用foreach
    let searchByForEach = (arr, value) => {
      let result = [];
      arr.forEach((item, i) => {
        if (item === value) result.push(i);
      });
      return result
    };
  }; {
    // while循环
    let searchByWhile = (arr, value) => {
      let i = arr.length, result = [];
      while (i) {
        if (arr[i] === value) {
          result.push(i);
        }
        i--;
      };
      return result;
    };
  }
</script>

</html>